package sys.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sys.core.annotation.RecordLog;
import sys.dao.OperationMapper;
import sys.entity.OperationEntity;
import sys.entity.User;
import sys.utils.IPUtils;
import sys.utils.SessionUtil;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Component
public class LogAopAspect {


    @Autowired
    private OperationMapper operationDao;


    private static final Logger logger = LoggerFactory.getLogger(LogAopAspect.class);


    @Around("@annotation(sys.core.annotation.RecordLog)")
    public Object process(ProceedingJoinPoint pjp) throws Throwable {
        Class<?> currentClass = pjp.getTarget().getClass();
        MethodSignature signature = (MethodSignature) (pjp.getSignature());
        String className = currentClass.getSimpleName();
        String methodName = currentClass.getMethod(signature.getName(), signature.getParameterTypes()).getName();
        Method method = signature.getMethod();
        RecordLog annotation = method.getAnnotation(RecordLog.class);
        String value = annotation.value();
        logger.info("操作名称:{}", value);
        ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (servletRequestAttributes == null) {
            logger.debug("请求异常，请稍后重试");
        }
        HttpServletRequest request = servletRequestAttributes.getRequest();
        User user = SessionUtil.getUser(request);
        logger.info("user:{}", user);
        String ip = IPUtils.getIpAddr(request);

      /*  Object[] args = pjp.getArgs();
        //将参数所在的数组转换成json
        String params = JSON.toJSONString(args);
        logger.info("========>param:{}",params);*/
        //保存到日志表
        OperationEntity operationEntity = new OperationEntity();
        if (user != null) {
            operationEntity.setCreateUser(Integer.valueOf(user.getId()));
            operationEntity.setCreateUsername(user.getUserName());
        }
        operationEntity.setOperName(value);
        operationEntity.setOperClass(className);
        operationEntity.setOperMethod(methodName);

        operationEntity.setIpAddress(ip);
        operationEntity.setCreateTime(new Date());
        //operationDao.add(operationEntity);
        operationDao.insert(operationEntity);
        logger.info("======= 开始执行：" + className + " — " + methodName + " ========");
        Object obj = pjp.proceed();
        logger.info("======= 执行结束：" + className + " — " + methodName + " ========");

        return obj;

    }


}
